<?php
// 加载通用文件
require_once __DIR__ . '/common/autoload.php';

try {
    echo "开始创建积分记录表...<br>";

    // 创建积分记录表
    $sql = "CREATE TABLE IF NOT EXISTS `points_log` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `user_id` int(11) NOT NULL COMMENT '用户ID',
        `points` int(11) NOT NULL COMMENT '积分变动值',
        `description` varchar(255) NOT NULL COMMENT '变动描述',
        `related_id` varchar(50) DEFAULT NULL COMMENT '关联ID（如订单ID）',
        `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
        PRIMARY KEY (`id`),
        KEY `user_id` (`user_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='积分记录表'";
    
    Database::query($sql);
    
    echo "积分记录表创建成功!<br>";
    
    // 为用户ID 29添加示例积分记录
    $user_id = 29;
    
    // 检查用户是否存在
    $check_user = Database::fetch("SELECT id FROM users WHERE id = ?", [$user_id]);
    if ($check_user) {
        echo "开始添加示例积分记录...<br>";
        
        // 添加积分记录示例
        $sample_data = [
            [
                'user_id' => $user_id,
                'points' => 100,
                'description' => '注册奖励',
                'related_id' => null,
                'created_at' => date('Y-m-d H:i:s', strtotime('-30 days'))
            ],
            [
                'user_id' => $user_id,
                'points' => 50,
                'description' => '消费奖励',
                'related_id' => '202505010001',
                'created_at' => date('Y-m-d H:i:s', strtotime('-15 days'))
            ],
            [
                'user_id' => $user_id,
                'points' => -20,
                'description' => '积分兑换',
                'related_id' => null,
                'created_at' => date('Y-m-d H:i:s', strtotime('-10 days'))
            ],
            [
                'user_id' => $user_id,
                'points' => 30,
                'description' => '生日奖励',
                'related_id' => null,
                'created_at' => date('Y-m-d H:i:s', strtotime('-5 days'))
            ],
            [
                'user_id' => $user_id,
                'points' => 25,
                'description' => '消费奖励',
                'related_id' => '202505020002',
                'created_at' => date('Y-m-d H:i:s', strtotime('-2 days'))
            ]
        ];
        
        foreach ($sample_data as $data) {
            $sql = "INSERT INTO points_log (user_id, points, description, related_id, created_at) 
                    VALUES (?, ?, ?, ?, ?)";
            Database::query($sql, [
                $data['user_id'],
                $data['points'],
                $data['description'],
                $data['related_id'],
                $data['created_at']
            ]);
        }
        
        echo "示例积分记录添加成功!<br>";
    } else {
        echo "用户ID {$user_id} 不存在，跳过添加示例数据。<br>";
    }
    
    echo "全部操作完成，请返回继续使用系统。";
    
} catch (Exception $e) {
    echo "错误: " . $e->getMessage();
}
?> 